package com.esg.esgproject.mp.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.esg.esgproject.mp.entity.auto.Company;
import com.esg.esgproject.mp.entity.auto.User;
import com.esg.esgproject.mp.qo.QoCompany;
import com.esg.esgproject.mp.qo.QoUser;
import com.esg.esgproject.mp.service.CompanyService;
import com.esg.esgproject.mp.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author z100
 * @since 2021-12-15
 */
@RestController
@RequestMapping("/user")
public class UserController {

    @Autowired
    UserService userService;

    /**
     *删
     * @param name 用户名
     * @param password 密码
     * @return
     */

    @GetMapping("/regist")
    public ResponseEntity<Object> regist(@RequestParam("name")String name,@RequestParam("password")String password){
        LambdaQueryWrapper<User> wrapper=new LambdaQueryWrapper<>();
        wrapper.eq(User::getName,name);
        User one = userService.getOne(wrapper);

        //如果用户名已经存在返回失败
        if(one!=null)
            return new ResponseEntity<>(HttpStatus.FAILED_DEPENDENCY);

        User registedUser=new User();
        registedUser.setName(name);
        registedUser.setPassword(password);
        userService.save(registedUser);

        return new ResponseEntity<>(HttpStatus.OK);

    }

    /**
     *删
     * @param name 用户名
     * @param password 密码
     * @return
     */
    @GetMapping("/login")
    public ResponseEntity<Object> login(@RequestParam("name")String name,@RequestParam("password")String password){
        LambdaQueryWrapper<User> wrapper=new LambdaQueryWrapper<>();
        wrapper.eq(User::getName,name);
        User one = userService.getOne(wrapper);

        if(one==null)//如果用户名不存在返回失败
            return new ResponseEntity<>(HttpStatus.FAILED_DEPENDENCY);
        if(!one.getPassword().equals(password))//如果密码错误返回失败
            return new ResponseEntity<>(HttpStatus.FAILED_DEPENDENCY);

        return new ResponseEntity<>(HttpStatus.OK);
    }

    @PutMapping("/add")
    public ResponseEntity<Object> add(User user){
        boolean res = userService.save(user);
        return res?new ResponseEntity<>(HttpStatus.OK):new ResponseEntity<>(HttpStatus.FAILED_DEPENDENCY);
    }

    @DeleteMapping("/delete")
    public ResponseEntity<Object> delete(@RequestParam("id") Long id){
        boolean res = userService.removeById(id);
        return res?new ResponseEntity<>(HttpStatus.OK):new ResponseEntity<>(HttpStatus.FAILED_DEPENDENCY);
    }

    @GetMapping("/getById")
    public ResponseEntity<Object> getById(Integer id){
        LambdaQueryWrapper<User> wrapper= new LambdaQueryWrapper<>();
        wrapper.eq(User::getId,id);
        User user = userService.getOne(wrapper);
        return new ResponseEntity<>(user, HttpStatus.OK);
    }


    @GetMapping("/get")
    public ResponseEntity<Object> search(QoUser qouser){
        LambdaQueryWrapper<User> wrapper= new LambdaQueryWrapper<>();
        wrapper.eq(User::getName,qouser.getName());
        List<User> list = userService.list(wrapper);
        return new ResponseEntity<>(list, HttpStatus.OK);
    }

    @GetMapping("/getAll")
    public ResponseEntity<Object> getAll(){
        List<User> all = userService.list();
        return new ResponseEntity<>(all, HttpStatus.OK);
    }


    @GetMapping("/update")
    public ResponseEntity<Object> update(User user,Long oldId){
        LambdaQueryWrapper<User> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(User::getId,oldId);
        boolean res = userService.update(user, wrapper);
        return res?new ResponseEntity<>(HttpStatus.OK):new ResponseEntity<>(HttpStatus.FAILED_DEPENDENCY);
    }

}
